package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.util.DateUtil;
import com.junzhiit.nengji.bo.OrdersBO;
import com.junzhiit.nengji.cms.rabbitmq.RabbitMQService;
import com.junzhiit.nengji.cms.service.CommissionHistoryService;
import com.junzhiit.nengji.cms.service.OrdersService;
import com.junzhiit.nengji.cms.service.RebateService;
import com.junzhiit.nengji.cms.web.exception.CheckOrdersNotFoundException;
import com.junzhiit.nengji.cms.web.exception.LogisticsNotFoundException;
import com.junzhiit.nengji.cms.web.exception.MobilePhoneCodeErrorException;
import com.junzhiit.nengji.cms.web.exception.MobilePhoneCodeTimeoutException;
import com.junzhiit.nengji.cms.web.exception.OrdersCodeErrorException;
import com.junzhiit.nengji.cms.web.exception.OrdersNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.Constants;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.LogisticsUtil;
import com.junzhiit.nengji.cms.web.util.aliyun.bean.LogisticsList;
import com.junzhiit.nengji.cms.web.util.aliyun.bean.LogisticsResultDate;
import com.junzhiit.nengji.cms.web.util.wechat.TemplateMessageUtil;
import com.junzhiit.nengji.cms.web.util.wechat.bean.TemplateMessageParam;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.OrdersCheckVO;
import com.junzhiit.nengji.cms.web.vo.OrdersCodeVO;
import com.junzhiit.nengji.cms.web.vo.OrdersListVO;
import com.junzhiit.nengji.cms.web.vo.OrdersPriceVO;
import com.junzhiit.nengji.cms.web.vo.OrdersVO;
import com.junzhiit.nengji.cms.web.vo.PhoneCodeVO;
import com.junzhiit.nengji.cms.web.vo.RebateVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.UserFormIdVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.LogisticsDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersDataHistoryDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.SystemConfigDAO;
import com.junzhiit.nengji.dao.UserCouponsDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.cache.UserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.Goods;
import com.junzhiit.nengji.model.Logistics;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersDataHistory;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.RebateInfo;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.SystemConfig;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.model.enums.ConfirmType;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.LoginType;
import com.junzhiit.nengji.model.enums.OrderGoodsStatus;
import com.junzhiit.nengji.model.enums.OrderStatus;
import com.junzhiit.nengji.model.enums.OrderType;

import cn.hutool.json.JSONUtil;

@Service
public class OrdersServiceImpl extends BaseServiceImpl implements OrdersService {

	@Autowired
	private OrdersDAO ordersDAO;

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private LogisticsDAO logisticsDAO;
	
	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;
	
	@Autowired
	private SystemConfigDAO systemConfigDAO;
	
	@Autowired
	private RebateService rebateService;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private ChainDAO chainDAO;
	
	@Autowired
	private UserCouponsDAO userCouponsDAO;
	
	@Autowired
	private UserCacheDAO userCacheDAO;
	
	@Autowired
	private OrdersDataHistoryDAO ordersDataHistoryDAO;
	
	@Autowired
	private CommissionHistoryService commissionHistoryService;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private RabbitMQService rabbitMQService;
	
	public Map<String, Object> findAll(DataTablesParamVO dtp, OrdersListVO ordersListVO) {
		OrdersBO ordersBO = new OrdersBO();
		BeanUtils.copyProperties(ordersListVO, ordersBO);
//		if (StringUtils.isBlank(ordersListVO.getShopId())) {
//			SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
//					.getHash(DIYKey.getSessionHashKey(ordersListVO.getSessionId()), SystemUserSessionVO.class);
//			if(StringUtils.isNotBlank(systemUserSessionVO.getSystemId())){
//				Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
//				Chain chain = chainDAO.query(systemUserSessionVO.getSystemId());
//				if (shop == null && chain == null) {
//					throw new ShopNotFoundException("店铺/品牌不存在");
//				}
//				if(shop != null){
//					ordersBO.setShopId(systemUserSessionVO.getSystemId());
//				}else if(chain != null){
//					ordersBO.setChainId(systemUserSessionVO.getSystemId());
//				}
//			}
//		}
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(ordersListVO.getSessionId()), SystemUserSessionVO.class);
		
		if(systemUserSessionVO.getLoginType() == LoginType.SHOP.toIntType()){
			if(systemUserSessionVO.getIsChain() == Is.NO.toIntValue()){
				Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
				if (shop == null) {
					throw new ShopNotFoundException("店铺不存在");
				}
				//商户查询时，设定该商户开启的经营类型
				if(ordersListVO.getType() == null){
					if(shop.getIsMail()==Is.YES.toIntValue()){
						ordersBO.setType(OrderType.MAIL.toIntValue());
					}else if(shop.getIsShop()==Is.YES.toIntValue()){
						ordersBO.setType(OrderType.SHOP.toIntValue());
					}else if(shop.getIsDelivery()==Is.YES.toIntValue()){
						ordersBO.setType(OrderType.DELIVERY.toIntValue());
					}else if(shop.getIsEat()==Is.YES.toIntValue()){
						ordersBO.setType(OrderType.EAT.toIntValue());
					}else if(shop.getIsReserve()==Is.YES.toIntValue()){
						ordersBO.setType(OrderType.RESERVE.toIntValue());
					}
				}
				
				ordersBO.setShopId(systemUserSessionVO.getSystemId());
			}else{
				Chain chain = chainDAO.query(systemUserSessionVO.getSystemId());
				if (chain == null) {
					throw new ShopNotFoundException("品牌不存在");
				}
				ordersBO.setChainId(systemUserSessionVO.getSystemId());
			}
		}

		int totalCount = ordersDAO.getTotalCount(ordersBO, false);
		List<OrdersBO> list = ordersDAO.findAll(dtp.getStart(), dtp.getLength(), ordersBO, false);
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	public OrdersVO findOrderId(String orderId) {
		OrdersBO orderBO = ordersDAO.findOrderId(orderId);
		if(orderBO==null){
			throw new OrdersNotFoundException("找不到订单");
		}
		
		OrdersVO orderVO = new OrdersVO();
		BeanUtils.copyProperties(orderBO, orderVO);

		if (orderBO.getPayWay() != null) {
			orderVO.setPayWay(orderBO.getPayWay().toString());
			orderVO.setPayWayInt(orderBO.getPayWay());
		}
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotBlank(orderBO.getAddressId())) {
			sb.append(orderBO.getArea()).append(" ").append(orderBO.getAddress());
			orderVO.setAddress(sb.toString());
		}
		orderVO.setStatusName(OrderStatus.toStatus(orderBO.getStatus()).toStringText());
		return orderVO;
	}

	@Transactional
	public ResultVO update(Orders order) {
		ResultVO result = new ResultVO();

		Integer status = order.getStatus();
		if(order.getLogisticsId() != null){
			Logistics logistics = logisticsDAO.query(order.getLogisticsId());
			if (logistics == null) {
				throw new LogisticsNotFoundException("该物流不存在！");
			}
//			LogisticsResultDate logisticsResultDate = LogisticsUtil.expressQuery(order.getLogisticsNumber(), logistics.getCode());
//			if(logisticsResultDate == null){
//				throw new LogisticsNumberErrorException("未查询到该物流单号");
//			}
		}
		
		if(status != null && (status == OrderStatus.FAIL.toIntValue())){
			List<OrdersGoods> list=ordersGoodsDAO.getOrderGoodsByOrderId(order. getId());
			for(int i=0;i<list.size();i++){
				list.get(i).setStatus(OrderGoodsStatus.REFUND_SUCCESS.toIntValue());
				ordersGoodsDAO.update(list.get(i));
			}
		}
		
		// order.setPayMoney(order.getPayMoney());
		if (status != null) {
			if (status == OrderStatus.PAID.toIntValue()) {
				order.setPayTime(new Date());// 付款时间
			} else if (status == OrderStatus.SHIPPED.toIntValue()) {
				order.setDeliveryTime(new Date());// 发货时间
			} else if (status == OrderStatus.CONFIRM.toIntValue()) {
				order.setConfirmTime(new Date());// 确认收货时间
			} else if (status == OrderStatus.COMPLETE.toIntValue()) {
				order.setCompleteTime(new Date());//订单完成时间
			}else if (status == OrderStatus.FAILIN.toIntValue()) { // 申请退款
				OrdersBO orders = ordersDAO.findOrderById(order.getId());
				if(orders != null && StringUtils.isNoneBlank(orders.getShopId())){
					Double money = orders.getPayMoney() - (orders.getPayMoney() * orders.getProfit() / 100);
					Shop shop = shopDAO.query(orders.getShopId());
					if((shop.getWaitSumMoney() - money) >= 0){
						shop.setWaitSumMoney(shop.getWaitSumMoney() - money);
					}
					shopDAO.update(shop);
				}
			} else if(status==OrderStatus.HASUSE.toIntValue()){
				OrdersBO data=ordersDAO.getcheckOrders(order);
				if(data==null){
					throw new CheckOrdersNotFoundException("核销订单未找到");
				}
				order.setUseTime(new Date());
				
				//检查是否免单商品，如果是核销成功加入队列
				SystemConfig systemConfig = systemConfigDAO.find();
				Goods goods = goodsDAO.query(data.getGoodsId());
				if(systemConfig.getIsOpenRebate() == Is.YES.toIntValue()){
					if(goods.getIsOpenRebate() == Is.YES.toIntValue()){
						RebateVO rebateVO = new RebateVO();
						rebateVO.setGoodsId(data.getId());
						rebateVO.setPrice(data.getPrice());
						rebateVO.setShopId(data.getShopId());
						rebateVO.setGoodsName(data.getGoodsName());
						rebateVO.setUserId(data.getUserId());
						RebateInfo rebateInfo = rebateService.addRebate(rebateVO);
						if(StringUtils.isNoneBlank(rebateInfo.getId())){
							rebateService.updateRedate(rebateInfo);
						}
					}
				}
				
				if(goods.getIsOpenSell() == Is.YES.toIntValue() && goods.getIsOpenRebate() == Is.NO.toIntValue()){
					User user = userDAO.query(data.getUserId());
					OrdersDataHistory orderDataHistory = ordersDataHistoryDAO.findByOrderId(order.getId());
					commissionHistoryService.add(data.getUserId(), data.getOrderGoodsId(), user.getParentId(),orderDataHistory.getId());
				}else if(goods.getIsOpenSell() == Is.NO.toIntValue() && goods.getIsOpenRebate() == Is.NO.toIntValue()){
					OrdersDataHistory orderDataHistory = ordersDataHistoryDAO.findByOrderId(order.getId());
					
					orderDataHistory.setStatus(Is.YES.toIntValue());
					ordersDataHistoryDAO.update(orderDataHistory);
				}
			}
		}
		boolean is = ordersDAO.update(order);
		if(is){
			if(status == OrderStatus.SHIPPED.toIntValue()){
				OrdersBO ordersBO = ordersDAO.findOrderId(order.getId());
				String key = DIYKey.getFormIdByOrderNumber(ordersBO.getOrderNumber());
				UserFormIdVO formIdVO = userCacheDAO.getHash(key, UserFormIdVO.class);
				if(formIdVO != null){
					Map<String, Object> data = TemplateMessageUtil.getMessageData(new String[]{ordersBO.getShopName(),
							ordersBO.getLogisticsName(),ordersBO.getLogisticsNumber(),
							DateUtil.format(ordersBO.getDeliveryTime()),
							ordersBO.getUserName(),ordersBO.getMobilePhone(),"如有疑问,请联系客服",ordersBO.getArea()+" "+ordersBO.getAddress()});
					TemplateMessageParam tmp = new TemplateMessageParam();
					tmp.setTouser(ordersBO.getOpenId());
					tmp.setTemplateId(TemplateMessageUtil.ORDERS_SEND);//模板ID
					tmp.setFormId(formIdVO.getFormId());
					tmp.setEmphasisKeyword("keyword3.DATA");
					tmp.setData(data);//内容
					TemplateMessageUtil.pullMessage(tmp);
				}
			}
		}
		return result;
	}

	@Override
	public ResultVO getOrderById(String orderId) {
		ResultVO result = new ResultVO();
		Orders orders = ordersDAO.query(orderId);
		result.setData(orders);
		return result;
	}

	@Override
	public ResultVO updateLogistics(String ordersId, String logisticsId, String logisticsNumber) {
		ResultVO result = new ResultVO();
		Orders orders = ordersDAO.query(ordersId);
		if(logisticsId != null){
			Logistics logistics = logisticsDAO.query(logisticsId);
			if (logistics == null) {
				throw new LogisticsNotFoundException("该物流不存在！");
			}
//			LogisticsResultDate logisticsResultDate = LogisticsUtil.expressQuery(logisticsNumber, logistics.getCode());
//			if(logisticsResultDate == null){
//				throw new LogisticsNumberErrorException("未查询到该物流单号");
//			}
		}
		if (orders != null && StringUtils.isNoneBlank(orders.getId())) {
			orders.setLogisticsId(logisticsId);
			orders.setLogisticsNumber(logisticsNumber);
			ordersDAO.update(orders);
		} else {
			throw new OrdersNotFoundException("找不到订单");
		}
		return result;
	}

	@Override
	@Transactional
	public ResultVO defaultRecerve() {
		ResultVO result = new ResultVO();
		
		List<Orders> list =ordersDAO.getShippedOrders();	
		List<Orders> ordersList=new ArrayList<Orders>();
		
		for (Orders orders : list) {
			LogisticsResultDate logisticsResultDate = LogisticsUtil.expressQuery(orders.getLogisticsNumber(),"auto");
			if(logisticsResultDate != null && logisticsResultDate.getResult().getDeliverystatus()==3){
				List<LogisticsList> resultList = logisticsResultDate.getResult().getList();
				
				//获得最新物流更新日期
				String Str= resultList.get(0).getTime();
				Date date= DateUtil.parseDateTime(Str);
				
				//获取七天前的日期
				Date dateParam=DateUtil.addDays(new Date(), -7);
				if(date.before(dateParam)){
					orders.setStatus(OrderStatus.CONFIRM.toIntValue());
					orders.setConfirmType(ConfirmType.SYSTEM.toIntValue());
					orders.setConfirmTime(new Date());
					ordersList.add(orders);
				}	
			}
		}
		if(ordersList!=null&&ordersList.size()>0){
			ordersDAO.batchUpdate(ordersList);	
		}
		return result;
	}
	
	@Override
	public ResultVO updateOrdersPrice(OrdersPriceVO ordersPriceVO) {
		Double priceDiffer=0.00;
		ResultVO result = new ResultVO();
		
		PhoneCodeVO phoneCode = systemUserCacheDAO.getHash(DIYKey.getMobilePhoneCodeKey(ordersPriceVO.getUserPhone()),
				PhoneCodeVO.class);
		if (!phoneCode.getCode().equals(ordersPriceVO.getPhoneCode())) {
			throw new MobilePhoneCodeErrorException("验证码不一致");
		}
		
		if (phoneCode.getTimestamp() < new Date().getTime()) {
			throw new MobilePhoneCodeTimeoutException("验证码已过期");
		}
		
		Orders orders=ordersDAO.query(ordersPriceVO.getOrderId());
		if(orders==null){
			throw new OrdersNotFoundException("找不到订单");
		}
		orders.setPayMoney(ordersPriceVO.getPayMoney());
		
		if(orders.getPriceDifference() != 0.00){
			priceDiffer = orders.getPriceDifference();
		}
		priceDiffer += ordersPriceVO.getPriceDifference();
		orders.setPriceDifference(priceDiffer);
		
		result.setData(ordersDAO.update(orders));
		return result;
	}
	
	@Override
	public ResultVO getcheckOrders(OrdersCheckVO ordersCheckVO){
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(ordersCheckVO.getSessionId()), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		Orders orders=new Orders();
		BeanUtils.copyProperties(ordersCheckVO, orders);
		orders.setShopId(shop.getId());
		
		OrdersBO data=ordersDAO.getcheckOrders(orders);
		
		if(data!=null){
			String validationCode=data.getValidationCode();
			StringBuilder vc=new StringBuilder();
			String replaceSymbol="*";
			if(StringUtils.isNotBlank(validationCode)){
				for (int i = 0; i<validationCode.length();i++){
		            char number = validationCode.charAt(i);
		            if (i>=validationCode.length()/3 && i<validationCode.length()/3*2){
		            	vc.append(replaceSymbol);
		            }else {
		            	vc.append(number);
		            }
		        }
				data.setValidationCode(vc.toString());
			}
		}
		result.setData(data);
		return result;
	}

	@Override
	public ResultVO checkOrdersCode(OrdersCodeVO ordersCodeVO) {
		ResultVO result=new ResultVO();
		OrdersBO ordersBO=new OrdersBO();
		if(ordersCodeVO.getId() == null || ordersDAO.findOrderId(ordersCodeVO.getId()) == null){
			throw new OrdersNotFoundException("找不到订单");
		}
		ordersBO=ordersDAO.findOrderId(ordersCodeVO.getId());
		
		if(!ordersCodeVO.getValidationCode().equals(ordersBO.getValidationCode())){
			throw new OrdersCodeErrorException("订单验证码不一致");
		}
		//线下订单过期，以后再做
//		if(ordersBO.getActiveDate().getTime() < new Date().getTime()){
//			throw new OrdersTimeoutException("该订单已过期");
//		}
		
		result.setData(ordersBO);
		
		return result;
	}

	@Override
	public void updateStatus() {
		List<Orders> list = ordersDAO.findByStatus(OrderStatus.WAIT.toIntValue());
		List<String> ordersIdList = new ArrayList<String>();
		for (Orders orders : list) {
			ordersIdList.add(orders.getId());
			int hours = DateUtil.hoursBetween(orders.getCreatedTime(), new Date());
			//超过24小时没有付款的订单
			if(hours >= Constants.HOURS){
				orders.setStatus(OrderStatus.CANCEL.toIntValue());
				ordersDAO.update(orders);
			}
		}
		userCouponsDAO.updateUserCoupons(ordersIdList);
	}

	@Override
	public ResultVO getPaidOrderCount(String sessionId) {
		ResultVO result=new ResultVO();
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		Integer count=ordersDAO.getPaidOrderCount(shop.getId());
		
		result.setData(count);
		return result;
	}

	@Override
	public ResultVO findCoupons(String orderId) {
		ResultVO result=new ResultVO();
		Orders orders = ordersDAO.query(orderId);
		result.setData(orders);
		return result;
	}

	@Override
	public void joinProperty() {
		Date date = DateUtil.parseDate(DateUtil.addDays(new Date(),-6), DateUtil.DATE_PATTERN_DEFAULT);
		List<OrdersBO> list = ordersDAO.getOrderByConfirmTime(date,Is.NO.toIntValue());
		for(OrdersBO ordersBO : list){
			//更新店铺资产
			if(ordersBO != null){
				Double sumGoodsMoney = ordersBO.getPrice() * ordersBO.getGoodsNumber();
				Double money = sumGoodsMoney - (sumGoodsMoney * ordersBO.getProfit() / 100);
				
				OrdersDataHistory ordersDataHistory = ordersDataHistoryDAO.findByOrderId(ordersBO.getId());
				if(ordersDataHistory != null){
					ordersDataHistory.setStatus(Is.YES.toIntValue());
					ordersDataHistoryDAO.update(ordersDataHistory);
				}
					
				Shop shop = shopDAO.query(ordersBO.getShopId());
				shop.setWithdrawalMoney(shop.getWithdrawalMoney()+money);
				shop.setAccumulativeMoney(shop.getAccumulativeMoney()+money);
				shop.setMonthIncomeMoney(shop.getMonthIncomeMoney()+money);
				if((shop.getWaitSumMoney() - money) > 0){
					shop.setWaitSumMoney(shop.getWaitSumMoney() - money);
				}
				shopDAO.update(shop);
				
				Map<String, Object> contentMap = new HashMap<>();
				contentMap.put("id", ordersBO.getId());
				contentMap.put("ps", 0);
				contentMap.put("areaId", shop.getAreaCountyId());
				contentMap.put("payAmount", ordersBO.getPayMoney());
				contentMap.put("shopOrderNumber", ordersBO.getShopOrderNumber());
				contentMap.put("shopId", shop.getId());
				contentMap.put("shopName", shop.getName());
				String content = JSONUtil.toJsonStr(contentMap);
				rabbitMQService.sendToStoreBusiness(ordersBO.getId(), content);
			}
		}
	}

	@Override
	public void confirmOrders() {
		//修改订单状态
		Date date = DateUtil.parseDate(DateUtil.addDays(new Date(),-1), DateUtil.DATE_PATTERN_DEFAULT);
		List<Orders> list = ordersDAO.findWaitConfirm(date,OrderStatus.SHIPPED.toIntValue());
		for(Orders orders : list){
			orders.setStatus(OrderStatus.COMPLETE.toIntValue());
			orders.setCompleteTime(new Date());
			ordersDAO.update(orders);
		}
		
		List<Orders> completelist = ordersDAO.findWaitConfirm(date,OrderStatus.COMPLETE.toIntValue());
		for(Orders orders : completelist){
			//修改订单商品能否退货/退款状态字段
			List<OrdersGoods> ordersGoodsList = ordersGoodsDAO.getOrderGoodsByOrderId(orders.getId());
			for(OrdersGoods ordersGoods : ordersGoodsList){
				ordersGoods.setCanRefund(Is.NO.toIntValue());
				ordersGoodsDAO.update(ordersGoods);
				//分销
				//资产表
				Goods goods = goodsDAO.query(ordersGoods.getGoodsId());
				if(goods.getIsOpenSell() == Is.YES.toIntValue() && goods.getIsOpenRebate() == Is.NO.toIntValue()){
					User user = userDAO.query(orders.getUserId());
					OrdersDataHistory orderDataHistory = ordersDataHistoryDAO.findByOrderId(ordersGoods.getId());
					commissionHistoryService.add(user.getId(), ordersGoods.getId(), user.getParentId(),orderDataHistory.getId());
				}else{
					OrdersDataHistory orderDataHistory = ordersDataHistoryDAO.findByOrderId(orders.getId());
					orderDataHistory.setStatus(Is.YES.toIntValue());
					ordersDataHistoryDAO.update(orderDataHistory);
				}
			}
		}
		
	}

	@Override
	public ResultVO findReserveInfo(String ordersId) {
		ResultVO result = new ResultVO();
		result.setData(ordersDAO.findReserveInfo(ordersId));
		return result;
	}
}
